/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.mBean.util;

import com.aptech.mBean.LoginController;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author LanhAnh
 */
@WebFilter(filterName = "loginFilter",
        urlPatterns = {"/faces/Admin/*"},
        initParams = {
    @WebInitParam(name = "mood", value = "awake")})
public class LoginFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        LoginController auth = (LoginController) req.getSession().getAttribute("loginController");
        if (auth != null && auth.getManagerLogin() != null) {
            // Manager is logged in, so just continue request.
            chain.doFilter(request, response);
        } else {
            // Manager is not logged in, so redirect to index.
            HttpServletResponse res = (HttpServletResponse) response;
            res.sendRedirect(req.getContextPath() + "/faces/adminLogin.xhtml");
        }
    }

    @Override
    public void destroy() {
        // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
